package com.example.administrator.easy.view;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


import com.example.administrator.easy.R;
import com.example.administrator.easy.application.MyApplication;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private FragmentTransaction transaction;

    private RadioGroup rg;

    private ArrayList<RadioButton> rbList;
    private ArrayList<Fragment> fmList;
    private int curIndex,tarIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        rg = (RadioGroup) findViewById(R.id.main_rg);
        rbList = new ArrayList<>();
        Log.e("Main","count"+rg.getChildCount());
        for(int i =0;i<rg.getChildCount();i++){
            rbList.add((RadioButton) rg.getChildAt(i));
        }
        //默认选中第一个
        rbList.get(0).setChecked(true);
        //实例化碎片
        fmList = new ArrayList<>();
        fmList.add(new GuangFragment());
        fmList.add(new ZhuiFragment());
        fmList.add(new ShangFragment());
        //判断是否登录，是显示。。，否显示。。
        MyApplication a = (MyApplication) getApplication();
        if(a.isLogin()){//登录
            fmList.add(new WodeFragment());
        }else{
            fmList.add(new WodeFragment());
        }
//
        //
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.main_ll,fmList.get(0));//默认添加第一个
        Log.e("Main",fmList.get(0)+"");
        transaction.commit();
        curIndex = 0;//默认当前碎片为第一个发现
        //rg的选中改变事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for(int x=0;x<rbList.size();x++){
                    if(rbList.get(x).getId()==checkedId){
                        tarIndex = x;  //设置目标索引
                        Log.e("Main","目标"+tarIndex+"");
                        break;
                    }
                }
                //切换碎片
                changeFragment();
            }
        });
    }

    //切换碎片
    private void changeFragment(){

        //验证当前显示的碎片和要切换的碎片是否为同一个
        if(curIndex!=tarIndex){//不是同个则切换
            Fragment curF = fmList.get(curIndex);
            Log.e("Main","当前"+curF+"");
            Fragment tarF = fmList.get(tarIndex);
            Log.e("Main","目标"+tarF+"");
            //要用getSupportFragmentManager
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if(tarF.isAdded()){//如果目标已经添加
                //隐藏当前显示目标
                transaction.hide(curF).show(tarF);
            }else {//如果目标未添加
                //隐藏当前添加目标
                transaction.hide(curF).add(R.id.main_ll,tarF);
            }
            transaction.commit();
            curIndex = tarIndex;
            Log.e("Main","当前的"+curIndex);
        }else{//已经在当前页了
            Toast.makeText(this,"已经是当前页啦", Toast.LENGTH_LONG).show();
        }
    }


}
